package com.gruppsex.trainingdiary;

import java.util.ArrayList;
import java.util.zip.Inflater;

import com.gruppsex.trainingdairy.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseBooleanArray;
import android.view.ContextMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class WorkOutActivities extends Activity {

	String[] activitys = new String[] {};
	ListView listView;
	ArrayAdapter<String> adapter;
	Intent myIntent;
	DataBaseHelper dbHelper = new DataBaseHelper(this);

	String activity = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.fadein, R.anim.fadeout);
		setContentView(R.layout.workoutactivities);

		dbHelper.openDataBase();
		activitys = dbHelper.getActivity();
		dbHelper.close();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, activitys);
		listView = (ListView) findViewById(R.id.listViewActivities);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setAdapter(adapter);
	}

	public void onClickActivitys(View v) {
		switch (v.getId()) {
		case R.id.buttonAdd:
			SparseBooleanArray checked = listView.getCheckedItemPositions();
			ArrayList<String> selectedItems = new ArrayList<String>();
			for (int i = 0; i < checked.size(); i++) {
				// Item position in adapter
				int position = checked.keyAt(i);
				// Add sport if it is checked i.e.) == TRUE!
				if (checked.valueAt(i))
					selectedItems.add(adapter.getItem(position));
			}
			String[] outputStrArr = new String[selectedItems.size()];
			for (int i = 0; i < selectedItems.size(); i++) {
				outputStrArr[i] = selectedItems.get(i);
			}
			myIntent = new Intent(v.getContext(), TestActivity.class);
			myIntent.putExtra("WorkOutActivity.activitys", outputStrArr);
			v.getContext().startActivity(myIntent);
			break;

		case R.id.buttonAddOwn:

			final Dialog dialog = new Dialog(this);
			dialog.setTitle("Add activity");
			dialog.setCancelable(false);
			dialog.setContentView(R.layout.customdialog);

			Button dialogButtonCancell = (Button) dialog
					.findViewById(R.id.dialogButtonCancell);
			dialogButtonCancell.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			final EditText ownActivity = (EditText) dialog
					.findViewById(R.id.et_ownactivity);

			Button dialogButtonOk = (Button) dialog
					.findViewById(R.id.dialogButtonOK);
			dialogButtonOk.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (activity != "") {
						dbHelper.openDataBase();
						dbHelper.createActivityEntry(activity);
						dbHelper.close();
						Intent i = new Intent(WorkOutActivities.this,
								WorkOutActivities.class); // your class
						startActivity(i);
						finish();
					}
				}
			});
			ownActivity.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					activity = s.toString();
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub

				}
			});

			dialog.show();
			break;
		}
	}
}
